<template>
  <div class="login-wrap">
    <el-form label-position="left" ref="ruleForm" label-width="0px" class="demo-ruleForm login-container">
      <h3 class="title1">二手交易平台后台管理系统</h3>
      <div class="center container">
      <div class="login-box">
        <div class="title">
          <span v-for="(item, index) in loginList" @click="loginControl(index)"  :key="index" :class="{'agent':index === curIndex}">{{item}}</span>
        </div>
        <div class="title-body">
          <div class="login-div" v-show="curIndex === 0">
            <div class="inputbox">
              <input
                type="text"
                placeholder="手机号码"
                v-model="login.phone"
              />
            </div>

            <div class="inputbox">
              <input type="password" placeholder="密码" v-model="login.password" />
            </div>
            <div class="loginbtn" @click="loginClick">登录</div>
          </div>
          <div class="register-div" v-show="curIndex === 1">
            <div class="inputbox">
              <input
                type="text"
                placeholder="用户昵称"
                v-model="register.nickName"
              />
            </div>
            <div class="inputbox">
              <input
                type="text"
                placeholder="手机号码"
                v-model="register.phone"
              />
            </div>
            <div class="inputbox">
              <input type="password" placeholder="密码" v-model="register.password" />
            </div>
            <div class="loginbtn" @click="registerClick">注册</div>
          </div>
        </div>
        <div class="message">
          <div class="forget">
            <span class="register" @click="curIndex=1" v-show="curIndex===0">立即注册</span>
          </div>
        </div>
      </div>
      </div>
    </el-form>
  </div>
</template>
<script>
import { mapMutations, mapState } from 'vuex';
export default {
  name: 'login',
  data() {
    return {
      curIndex: 0,
      loginList: ['账号登录', '账号注册'],
      login: { // 登录所需参数
        phone: '',
        password: ''
      },
      register: { // 注册所需参数
        nickName: '',
        phone: '',
        password: ''
      }
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created() {
  },
  methods: {
    ...mapMutations(['setUser']),
    loginControl (index) {
      this.curIndex = index
    },
    loginClick() {
      if (!this.login.phone) {
        this.$message({
          message: '商品号为空，请正确输入',
          type: 'warning'
        });
        return
      }
      if (!this.login.password) {
        this.$message({
          message: '密码为空，请正确输入',
          type: 'warning'
        });
        return
      }
      this.$api.auth.login(this.login).then(res => {
        if (res.code === 200) {
          this.setUser(res.data)
          this.$router.push("/index");

        }
      })
    },
    registerClick() {
      if (!this.register.nickName) {
        this.$message({
          message: '用户昵称为空，请正确输入',
          type: 'warning'
        });
        return
      }
      if (!this.register.phone) {
        this.$message({
          message: '商品号为空，请正确输入',
          type: 'warning'
        });
        return
      }
      if (!this.register.password) {
        this.$message({
          message: '密码为空，请正确输入',
          type: 'warning'
        });
        return
      }
      this.$api.auth.register(this.register).then(res => {
        if (res.code === 200) {
          this.this.curIndex = 0
        }
      })
    }
  }
}
</script>

<style scoped>
.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding-top: 10%;
  background-image: url();
  /* background-color: #112346; */
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}
.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 400px;
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}
.title1 {
  margin: 0px auto 0 auto;
  text-align: center;
  color: #505458;
  font-size: 20px;
}
.remember {
  margin: 0px 0px 35px 0px;
}
.code-box {
  text-align: right;
}
.codeimg {
  height: 40px;
}
.center {
    position: relative;
    height: 450px;
  }
.login-box {
    position: absolute;
    width: 410px;
    height: 400px;
    background-color: #fff;
    right: 0;
    top: 35px;
    padding: 0 30px;
    box-sizing: border-box;
    /* border: 1px solid #ff6600; */
  }
  .title {
      margin: 40px 0;
      display: flex;
      text-align: center;
    }
.title span {
    flex: 1;
    height: 25px;
    line-height: 25px;
    font-size: 20px;
  }  
.title span:first-child{
  border-right: 2px solid #d7d7d7;
}
.agent {
color: #ff6600;
}
.title-body .inputbox {
  width: 300px;
  height: 50px;
  border: 2px solid #eee;
  margin-top: 20px;
  
}
.title-body .inputbox input {
  width: 100%;
  height: 100%;
  border: none;
  text-indent: 20px;
  line-height: 50px;
}
.loginbtn {
  width: 348px;
  height: 50px;
  background-color: #ff6600;
  margin-top: 30px;
  text-align: center;
  line-height: 50px;
  font-size: 16px;
  color: #fff;
  cursor: pointer;
}
.message {
  display: flex;
  margin-top: 10px;
}
.number {
  flex: 1;
  color: #ff6600;
}
.forget {
  flex: 1;
  text-align: right;
}
.forget span {
  padding: 0 10px;
  color: #d7d7d7;
}
.forget span:nth-child(2) {
  padding-right: 0;
  border-left: 1px solid #eee;
}
.register {
  cursor: pointer;
}
.register:hover {
  color: #ff6600;
}

.header {
  height: 112px;
  background-color: #fff;
}
.header > a {
  display: inline-block;
}
img {
  margin-top: 4px;
}

.footer {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 80px;
}
.footer .info {
  height: 20px;
  line-height: 20px;
  font-size: 14px;
  margin: 15px 0;
}
.footer .info a {
  color: #333;
  padding: 0 15px;
  border-right: 1px solid #666;
}
.footer .copyright {
  height: 20px;
  line-height: 20px;
  font-size: 14px;
}
</style>
